﻿using System;
using UnityEngine;

[Serializable]
public class PlayerManager {

    public GameObject playerPrefab;
    public Transform spawnPoint;
    //public Color playerColor;
    public IJoyStick joyStick;

    [HideInInspector]
    public GameObject instance;
    [HideInInspector]
    public bool isSetup;

    private TankMove _tankMove;
    private TankShooting _tankShoot;
    private AvoidHorizontal _tankAvoid;
    private TankHealth _tankHealth;

	public void SetUp()
    {
        _tankMove = instance.GetComponent<TankMove>();
        _tankShoot = instance.GetComponent<TankShooting>();
        _tankAvoid = instance.GetComponent<AvoidHorizontal>();
        _tankHealth = instance.GetComponent<TankHealth>();

        _tankMove.joyStick = joyStick;
        _tankShoot.joyStick = joyStick;
        _tankAvoid.joyStick = joyStick;

        isSetup = true;

        /*
        MeshRenderer[] renders = instance.GetComponentsInChildren<MeshRenderer>();
        for(int i=0; i<renders.Length; i++)
        {
            renders[i].material.color = playerColor;
        }
        */
    }

    public void Reset()
    {
        instance.transform.position = spawnPoint.position;
        instance.transform.rotation = spawnPoint.rotation;
        instance.SetActive(false);
        instance.SetActive(true);
    }

    public bool IsAlive()
    {
        return !_tankHealth.isDead;
    }
}
